package cn.tedu.tmall.passport.redis;

import lombok.Data;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;
import java.util.Map;

@Data
class Point implements Serializable {
    private static final long serialVersionUID = 88497004682397228L;
    private int x;
    private int y;
}

@SpringBootTest
public class RedisTests1 {
    @Autowired
    RedisTemplate redisTemplate;
    @Test
    void redisStr(){
        //redisTemplate.setKeySerializer(RedisSerializer.string());
        //redisTemplate.setValueSerializer(RedisSerializer.json());
        ValueOperations vo = redisTemplate.opsForValue();
        Point p1 = new Point();
        p1.setX(10);
        p1.setY(20);
        vo.set("p1",p1);
        Point p2 = (Point) vo.get("p1");
        System.out.println(p1==p2);
        System.out.println(p1.equals(p2));
    }

    @Test
    void redisHash(){
        //redisTemplate.setKeySerializer(RedisSerializer.string());
        //redisTemplate.setValueSerializer(RedisSerializer.json());
        //redisTemplate.setHashKeySerializer(RedisSerializer.string());
        //redisTemplate.setHashValueSerializer(RedisSerializer.json());
        HashOperations vo = redisTemplate.opsForHash();
        vo.put("cart","good1",3);
        vo.put("cart","good2",5);
        Object o = vo.get("cart", "good1");
        System.out.println(o);
        Map cart = vo.entries("cart");
        System.out.println(cart);
    }
}
